import json


def number_choose(s):
    try:
        return float(s)
    except ValueError:
        if "/" in s:
            s = s.split("/")
            try:

                return float(s[0]) / float(s[1])
            except ValueError:
                return False
        return False


new_question = json.load(open("new_question.json", encoding="utf-8"))
print(len(new_question))
calculation_symbols = ["+", "-", "*", "/", "//", "%"]
revert_symbols = ["-", "/", "//", "%"]
three_calculation_symbols = []

for calculation_symbol in calculation_symbols:
    for calculation_second in calculation_symbols:
        three_calculation_symbols.append([calculation_symbol, calculation_second])
third_mathematics_practical_problems = []


def add(a, b):
    return a + b


def remove(a, b):
    return a - b


def division(a, b):
    return a / b


def multiplication(a, b):
    return a * b


def division_int(a, b):
    return a // b


def five(a, b):
    return a % b


def three_calculation_compute(number, three_calculation_label):
    computer_answer = None
    if number[0] and number[1]:
        if three_calculation_label[0] == "+":
            computer_answer = add(number[0], number[1])
        if three_calculation_label[0] == "-":
            computer_answer = remove(number[0], number[1])
        if three_calculation_label[0] == "*":
            computer_answer = multiplication(number[0], number[1])
        if three_calculation_label[0] == "/":
            computer_answer = division(number[0], number[1])
        if three_calculation_label[0] == "//":
            computer_answer = division_int(number[0], number[1])
        if three_calculation_label[0] == "%":
            computer_answer = five(number[0], number[1])
    return computer_answer


slot_question = []
unbelived_data = []
for new_question in new_question:
    if new_question in slot_question:
        continue
    else:
        slot_question.append(new_question)
    match_answer = number_choose(new_question["answer"])
    new_question["compute_answer"] = match_answer
    new_question["compute_answers"] = []

    new_question["label"] = []
    if len(new_question["number"]) == 1:
        continue
    if len(new_question["number"]) == 2:
        calculation = new_question["number"]
        for calculation_label in range(len(calculation_symbols)):
            first_answer = three_calculation_compute(calculation, calculation_symbols[calculation_label])
            if first_answer == match_answer:
                new_question["label"].append({str(calculation_label + 1): calculation_symbols[calculation_label]})
                if new_question not in third_mathematics_practical_problems:
                    third_mathematics_practical_problems.append(new_question)
                break
            else:
                new_question["compute_answers"].append({str(calculation_label + 1): first_answer})

        for revert_index in range(len(revert_symbols)):
            first_answer = three_calculation_compute([calculation[1], calculation[0]],
                                                     revert_symbols[revert_index])
            if first_answer == match_answer:
                new_question["label"].append({str(revert_index + len(calculation_symbols) + 1): revert_symbols[revert_index]})
                if new_question not in third_mathematics_practical_problems:
                    third_mathematics_practical_problems.append(new_question)
                break
            else:
                new_question["compute_answers"].append({str(revert_index + len(calculation_symbols) + 1): first_answer})
    if len(new_question["number"]) == 3:
        for three_calculation_index in range(len(three_calculation_symbols)):
            compute_answer = three_calculation_compute([new_question["number"][0], new_question["number"][1]],
                                                       three_calculation_symbols[three_calculation_index][0])
            second_compute_answer = three_calculation_compute([compute_answer, new_question["number"][2]],
                                                              three_calculation_symbols[three_calculation_index][1])
            if second_compute_answer == match_answer:
                new_question["label"].append({str(
                    three_calculation_index + len(calculation_symbols) + 1 + len(revert_symbols)):
                                                  three_calculation_symbols[three_calculation_index]}
                                             )
                if new_question not in third_mathematics_practical_problems:
                    third_mathematics_practical_problems.append(new_question)
                break
            else:
                new_question["compute_answers"].append({str(
                    three_calculation_index + len(calculation_symbols) + 1 + len(
                        revert_symbols)): second_compute_answer})
    if not new_question["label"]:
        unbelived_data.append(new_question)

print(len(third_mathematics_practical_problems))
json.dump(third_mathematics_practical_problems,
          open("fifth_mathematics_practical_problem.json", "w", encoding="utf-8"), ensure_ascii=False, indent=3)
json.dump(third_mathematics_practical_problems[:20],
          open("fifth_mathematics_practical_part.json", "w", encoding="utf-8"), ensure_ascii=False, indent=3)
json.dump(unbelived_data,
          open("fifth_unbelieved_data.json", "w", encoding="utf-8"), ensure_ascii=False, indent=3)
print(len(third_mathematics_practical_problems) / len(slot_question))
